package br.com.fiap.trabalho.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.fiap.trabalho.entity.Category;

public class JPACategoryDAO  extends JPADAO<Category> implements CategoryDAO {

	public JPACategoryDAO(EntityManager em) {
		super(em);
	}

	public Category createCategory(Category category) {
		return persist(category);
	}

	public boolean deleteCategory(Category category) {
		return delete(category);
	}

	@SuppressWarnings("unchecked")
	public List<Category> selectCategoryByName(String name) {
		Query query = em.createQuery(" SELECT c from Category c where c.name = :categoryName ");
		query.setParameter("categoryName", name);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Category> selectCategories(int numberOfElements) {
	
		Query query = em.createQuery(" SELECT c from Category c");
		
		query.setMaxResults(numberOfElements);
		
		return query.getResultList();
	}
}

